function out = BOHitOrMissE2D(im,method)
%%  BOHitOrMissE - end and triple points detection based on erosion
%   
%   REFERENCE:
%       C. Di Ruberto and A.G. Dempster,
%       Attributed skeleton graphs using mathematical morphology, 
%       ELECTRONICS LETTERS, 37, 22, 1325-1327, 2001
%
%   INPUT:
%       im      - binary image.
%       method  - 'end' or 'triple'.
%
%   OUTPUT:
%       out     - image with detected points
%
%   USAGE:
%        % Skeleton
%        sk = bwmorph(im, 'thin', inf);
%        % Hit or Miss
%        out1 = BOHitOrMissE(sk, 'end');
%        out2 = BOHitOrMissE(sk, 'triple');
% 
%   AUTHOR:
%       Boguslaw Obara, http://boguslawobara.net/
%
%   VERSION:
%       0.1 - 25/08/2008 First implementation

%% SE - endpoints 
if strcmp(method, 'end')
se(:,:,1) = [    0  1  0   ;...
                 0  1  0   ;...
                 0  0  0   ];
            
se(:,:,2) = [    0  0  1   ;...
                 0  1  0   ;...
                 0  0  0   ];
            
se(:,:,3) = [    0  0  0   ;...
                 0  1  1   ;...
                 0  0  0   ];
            
se(:,:,4) = [    0  0  0   ;...
                 0  1  0   ;...
                 0  0  1   ];
            
se(:,:,5) = [    0  0  0   ;...
                 0  1  0   ;...
                 0  1  0   ];
            
se(:,:,6) = [    0  0  0   ;...
                 0  1  0   ;...
                 1  0  0   ];
            
se(:,:,7) = [    0  0  0   ;...
                 1  1  0   ;...
                 0  0  0   ];
            
se(:,:,8) = [    1  0  0   ;...
                 0  1  0   ;...
                 0  0  0   ];
%% SE - triple points (junctions)
elseif strcmp(method, 'triple')
se(:,:,1) = [    0  1  0   ;...
                 1  1  1   ;...
                 0  0  0   ];

se(:,:,2) = [    1  0  1   ;...
                 0  1  0   ;...
                 0  0  1   ];

se(:,:,3) = [    0  1  0   ;...
                 0  1  1   ;...
                 0  1  0   ];

se(:,:,4) = [    0  0  1   ;...
                 0  1  0   ;...
                 1  0  1   ];

se(:,:,5) = [    0  0  0   ;...
                 1  1  1   ;...
                 0  1  0   ];

se(:,:,6) = [    1  0  0   ;...
                 0  1  0   ;...
                 1  0  1   ];

se(:,:,7) = [    0  1  0   ;...
                 1  1  0   ;...
                 0  1  0   ];

se(:,:,8) = [    1  0  1   ;...
                 0  1  0   ;...
                 1  0  0   ];

se(:,:,9) = [    0  1  0   ;...
                 0  1  1   ;...
                 1  0  0   ];

se(:,:,10)= [    0  0  1   ;...
                 1  1  0   ;...
                 0  0  1   ];

se(:,:,11)= [    1  0  0   ;...
                 0  1  1   ;...
                 0  1  0   ];

se(:,:,12)= [    0  1  0   ;...
                 0  1  0   ;...
                 1  0  1   ];

se(:,:,13)= [    0  0  1   ;...
                 1  1  0   ;...
                 0  1  0   ];

se(:,:,14)= [    1  0  0   ;...
                 0  1  1   ;...
                 1  0  0   ];

se(:,:,15)= [    0  1  0   ;...
                 1  1  0   ;...
                 0  0  1   ];

se(:,:,16)= [    1  0  1   ;...
                 0  1  0   ;...
                 0  1  0   ];
end
%%  Hit or Miss
if strcmp(method, 'end') || strcmp(method, 'triple')
    out = zeros(size(im));
    for i=1:size(se,3)
        ero = imerode(im, se(:,:,i));
        out = imadd(out, double(ero));
    end
    out = out==1;
end
%% 
end